package net.knightwatch.kwtools.walkthrough.items;

import android.content.Context;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;

public class BooleanAnswer extends Answer {
	protected static final String TAG = "BooleanAnswer";
	// Fields
	private String trueText = "true";
	private String falseText = "false";
	private Boolean value;
	private String answer;
	private Switch switchObject;
	// constructor
	public BooleanAnswer(){
		this.setAnswerTypeId(BOOLEAN_TYPE);
		// default value set
		this.setValue(false);
	}
	// methods

	public String getTrueText() {
		return trueText;
	}
	/** 
	 * Sets the pair of of the translation from value to string. 
	 * Parameters required are the Text if the value is true and the Text value if it is false.
	 * @param truetext
	 * @param falsetext
	 */
	public void setValuesString(String truetext, String falsetext){
		setTrueText(truetext);
		setFalseText(falsetext);
	}
	public void setTrueText(String trueText) {
		this.trueText = trueText;
	}

	public String getFalseText() {
		return falseText;
	}

	public void setFalseText(String falseText) {
		this.falseText = falseText;
	}
	public Boolean getValue() {
		return value;
	}

	public void setValue(Boolean value) {
		this.value = value;
		// Set the string answer to the text
		if(this.value){
			setAnswer(trueText);
		} else {
			setAnswer(falseText);
		}
		Log.d(TAG, "The value changed to " + this.toString());
	}

	@Override
	public LinearLayout getAnswerLayout(Context context) {
		LinearLayout ll = new LinearLayout(context);
		switchObject = new Switch(context);
		switchObject.setTextOn(trueText);
		switchObject.setTextOff(falseText);
		switchObject.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton switchObject, boolean isSwitchChecked) {
				// set the value of the object
				setValue(isSwitchChecked);
			}
		});
		ll.addView(switchObject);
		return ll;
	}

	@Override
	public String toString() {
		return answer;
	}
	public String getAnswer(){
		return this.answer;
	}
	public void setAnswer(String string) {
		this.answer = string;
	}

	@Override
	public void updateAnswerView() {
		// TODO Update the boolean answer 
		
	}

	@Override
	public void setAnswerText(String answerText) {
		// TODO Auto-generated method stub
		setAnswer(answerText);
		if(trueText.equals(answerText)){
			switchObject.setChecked(true);
		} else {
			switchObject.setChecked(false);
		}
	}
}
